package com.neuedu.his.dao;


import com.neuedu.his.common.utils.DBUtils;
import com.neuedu.his.entity.UmsMenu;

import java.util.List;
import java.util.Map;

public class UmsMenuDao {

    /**
    * 查询集合
    */
    public List<UmsMenu> selectList(String where , Object ... params){
        String sql =" select * from ums_menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        List<UmsMenu> umsMenus = DBUtils.selectList(UmsMenu.class, sql, params);

        return umsMenus;
     }

    /**
    * 分页查询
    * @param where
    * @param pageNo
    * @param size
    * @param params
    * @return
    */
    public List<UmsMenu> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from ums_menu ";
        if(where != null && !"".equals(where)){
        sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<UmsMenu> umsMenus = DBUtils.selectList(UmsMenu.class, sql, params);

        return umsMenus;
    }


    /**
    * 查询单条结果
    */
    public UmsMenu selectOne(String where ,Object ... params){
        String sql =" select * from ums_menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(UmsMenu.class, sql, params);
    }


    /**
     * 查询总条数
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from ums_menu ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }




    /**
    * 根据数据进行插入
    * @param umsMenu
    * @return
    */
    public int insert(UmsMenu umsMenu){
        String sql = "insert into ums_menu(       level   ,       menu_name   ,       parent_id   ,       url   ) values(   ?   ,     ?   ,     ?   ,     ?    )";
        return DBUtils.update( sql,    umsMenu.getLevel()    ,     umsMenu.getMenuName()    ,     umsMenu.getParentId()    ,     umsMenu.getUrl() );
    }


    /**
    * 根据主键更新一条
    * @param umsMenu
    * @return
    */
    public int updateById(UmsMenu umsMenu){
        String sql = " update ums_menu set     del = ?   ,       level = ?   ,       menu_name = ?   ,       parent_id = ?   ,       url = ?   where  menu_id = ?  ";

        return DBUtils.update( sql,
                    umsMenu.getDel()  ,
                    umsMenu.getLevel()  ,
                    umsMenu.getMenuName()  ,
                    umsMenu.getParentId()  ,
                    umsMenu.getUrl()  ,
                    umsMenu.getMenuId() );
    }



    /**
    * 根据主键更新一条
    * @param id
    * @return
    */
    public int deleteById(Integer id){
        String sql = " delete from ums_menu  where menu_id = ?";
        return DBUtils.update( sql, id);

    }


}